import { gameHelper } from "../../utils/GameHelpler";

const { ccclass, property, requireComponent, executeInEditMode, menu } = cc._decorator;
@ccclass

@menu('cccframework/SpriteFillBind')
// @executeInEditMode
@requireComponent(cc.Sprite)
export class SpriteFillBind extends cc.Component {

    @property(cc.Node)
    icon: cc.Node = null;

    start() {
        this.node.getComponent(cc.Sprite).type = cc.Sprite.Type.FILLED;

    }

    update(deltaTime: number) {
        if (this.icon) {
            this.icon.position = gameHelper.getPositionToAlignNode(this.icon, this.node);
            let dist = 0;
            let sprite = this.node.getComponent(cc.Sprite)
            let transform = this.node
            if (sprite.fillType == cc.Sprite.FillType.HORIZONTAL) {
                dist = transform.width * sprite.fillRange - transform.width / 2;
                this.icon.position = this.icon.position.add(cc.v2(dist, 0));
            } else if (sprite.fillType == cc.Sprite.FillType.VERTICAL) {
                dist = transform.height * sprite.fillRange - transform.height / 2;
                this.icon.position = this.icon.position.add(cc.v2(0, dist));
            }
        }
    }

}
